package etc.com.market;

import java.util.ArrayList;

import etc.com.sql.DBAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ViewVideo extends Activity {
	ArrayList<String> text = new ArrayList<String>();
	int n;
	int[] imageIDs;
	private static final int REQUEST_CODE = 10;
	String storename;
	String str;
	String previous;
	WebView myWebView;
	Cursor cursor;

	// R.drawable.mandarin_orange, R.drawable.oranges, R.drawable.pineap,
	// R.drawable.pomegranate, R.drawable.sweetorange, R.drawable.tangelo

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.viewvideo);

		Bundle extras = getIntent().getExtras();

		if (extras == null) {
			return;
		}
		String prodId = extras.getString("productId");
		DBAdapter dbAdapter = new DBAdapter(getApplicationContext());
		cursor = dbAdapter.getProductDetailsById(prodId);
		cursor.moveToNext();
		String videoPath = cursor.getString(5);
		System.out.println("VideoPath: " + videoPath);
		cursor.close();
		WebView myWebView = (WebView) findViewById(R.id.webview);
		String summary = "<html><body><iframe width=\"303dp\" height=\"238dp\" src=\"http://www.youtube.com/embed/EEpuuasBOOc\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
		myWebView.getSettings().setPluginsEnabled(true); 
		myWebView.getSettings().setJavaScriptEnabled(true); 
		myWebView.loadData(summary, "text/html", null);

		String messageReceive = extras.getString("message");

		if (messageReceive != null) {
			// arrtext.add(messageReceive);
			str = messageReceive.toString();

			previous = str.substring(0, str.indexOf(";"));

			str = str.substring(str.indexOf(";") + 1);

			storename = str.substring(0, str.indexOf(";"));

			str = str.substring(str.indexOf(";") + 1);

			TextView view = (TextView) findViewById(R.id.storename);
			view.setText(storename);

//			TextView view1 = (TextView) findViewById(R.id.headerprevious);
//			view1.setText(previous);
		}


	}

	

	// function use for footer button ==============================
	public void Previous(View view) {
		finish();
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
		return;
	}

	public void Home(View view) {
		Intent i = new Intent(this, StoreTopPage.class);
		i.putExtra("message", storename);
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void SearchProduct(View view) {
		onClickSearch(view, ("Store Info;" + storename));
	}

	public void SpecifyProduct(View view) {
		onClickSpec(view, ("Store Info;" + storename));
	}

	public void Info(View view) {
		onClickInfo(view, storename);
	}
	
	public void Contact(View view) {
		onClickContact(view, storename);
	}

	// ===================================================================
	public void onClickPre(View view, String message) {
		Toast.makeText(this, "You clicked on go to product detail button", 1)
				.show();

		Intent i = new Intent(this, ProductDetail.class);

		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickSpec(View view, String message) {
		Intent i = new Intent(this, ListitemActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickSearch(View view, String message) {
		Intent i = new Intent(this, SearchOursProducts.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickInfo(View view, String message) {
		Intent i = new Intent(this, InfomationAct.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onClickContact(View view, String message) {
		Intent i = new Intent(this, SenmailActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public class ImageAdapter extends BaseAdapter {
		private Context context;

		public ImageAdapter(Context c) {
			context = c;
		}

		// ---returns the number of images---
		public int getCount() {
			return imageIDs.length;
		}

		// ---returns the ID of an item---
		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		// ---returns an ImageView view---
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView;
			if (convertView == null) {
				imageView = new ImageView(context);
				imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imageView.setPadding(5, 5, 5, 5);
			} else {
				imageView = (ImageView) convertView;
			}
			imageView.setImageResource(imageIDs[position]);
			return imageView;
		}
	}
}


